package com.sairui.xiaoruilibdemo;

import android.app.Activity;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


/**
 * Created by sunqiyong on 2017/7/24.
 */

public class BottomManager implements View.OnClickListener {
    private ImageView icon1;
    private ImageView icon2;
    private ImageView icon3;
    private ImageView icon4;
    private TextView name1;
    private TextView name2;
    private TextView name3;
    private TextView name4;
    private LinearLayout action1;
    private LinearLayout action2;
    private LinearLayout action3;
    private LinearLayout action4;
    private int[] iconY;
    private int[] iconN;
    private String[] names;

    private int color_y,color_n;

    private SelectListener mSelectListener;

    public void setSelectListener(SelectListener mSelectListener) {
        this.mSelectListener = mSelectListener;
    }

    public BottomManager(Activity context, int[] iconY, int[] iconN, String[] name, int color_y, int color_n) {
        this.icon1 = (ImageView) context.findViewById(R.id.icon1);
        this.icon2 = (ImageView) context.findViewById(R.id.icon2);
        this.icon3 = (ImageView) context.findViewById(R.id.icon3);
        this.icon4 = (ImageView) context.findViewById(R.id.icon4);
        this.name1 = (TextView) context.findViewById(R.id.name1);
        this.name2 = (TextView) context.findViewById(R.id.name2);
        this.name3 = (TextView) context.findViewById(R.id.name3);
        this.name4 = (TextView) context.findViewById(R.id.name4);
        action1 = (LinearLayout) context.findViewById(R.id.action1);
        action2 = (LinearLayout) context.findViewById(R.id.action2);
        action3 = (LinearLayout) context.findViewById(R.id.action3);
        action4 = (LinearLayout) context.findViewById(R.id.action4);
        action1.setOnClickListener(this);
        action2.setOnClickListener(this);
        action3.setOnClickListener(this);
        action4.setOnClickListener(this);
        name1.setText(name[0]);
        name2.setText(name[1]);
        name3.setText(name[2]);
        name4.setText(name[3]);
        this.color_y = ContextCompat.getColor(context,color_y);
        this.color_n = ContextCompat.getColor(context,color_n);
        this.iconY = iconY;
        this.iconN = iconN;
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.action1) {
            initStatus(0);
            mSelectListener.clickPosition(0);

        } else if (i == R.id.action2) {
            initStatus(1);
            mSelectListener.clickPosition(1);

        } else if (i == R.id.action3) {
            initStatus(2);
            mSelectListener.clickPosition(2);

        } else if (i == R.id.action4) {
            initStatus(3);
            mSelectListener.clickPosition(3);

        }
    }

    private void initStatus(int position){
        icon1.setImageResource(position == 0 ? iconY[0] : iconN[0]);
        icon2.setImageResource(position == 1 ? iconY[1] : iconN[1]);
        icon3.setImageResource(position == 2 ? iconY[2] : iconN[2]);
        icon4.setImageResource(position == 3 ? iconY[3] : iconN[3]);
        name1.setTextColor(position == 0 ? color_y : color_n);
        name2.setTextColor(position == 1 ? color_y : color_n);
        name3.setTextColor(position == 2 ? color_y : color_n);
        name4.setTextColor(position == 3 ? color_y : color_n);
    }

    public interface SelectListener{
        void clickPosition(int position);
    }

    public void setCurrentPosition(int mCurrentPosition) {
        initStatus(mCurrentPosition);
        mSelectListener.clickPosition(mCurrentPosition);
    }
}
